<?php
load_info_var($cf_controller);	   //加载smarty变量
load_file('goods_class', LIB_PATH);
$main = new Goods($db);
$id = isset($_POST['id']) ? intval($_POST['id']) : intval($_GET['id']);
$self = isset($_POST['self']) ? trim($_POST['self']) : trim($_GET['self']);
$return_page = $cf_pre_page ? $cf_pre_page : $cf_self_page;

require_once(INFO_PATH.'cf_brand.php');
require_once(INFO_PATH.'cf_goods_cate.php');
require_once(INFO_PATH.'cf_expressfee.php');
require_once(INFO_PATH.'cf_color.php');

switch ($cf_action) {
    case "index":
	$cate = add_array_item(array('' => '全部'), $cf_info_var_common['goods_cate']);
	$smarty->mypage($cf_controller, $cf_action, 0);	
	$skey = isset($_POST['skey']) ? trim($_POST['skey']) : urldecode($_GET['skey']);
	$cate = isset($_POST['cate']) ? trim($_POST['cate']) : urldecode($_GET['cate']);
	$brand = isset($_POST['brand']) ? trim($_POST['brand']) : urldecode($_GET['brand']);
	$start = isset($_POST['start']) ? trim($_POST['start']) : urldecode($_GET['start']);
	$end = isset($_POST['end']) ? trim($_POST['end']) : urldecode($_GET['end']);	
	$where = '1';
	if ($skey) {
	    $where .= " and goods_name like '%" . $skey . "%'";
	}	
	if ($start && $end) {
            $where .= " AND DATE_FORMAT(goods_create_time,'%Y-%m-%d')  BETWEEN '".$start."' AND '".$end."'";
    }
	
	if ($cate) {
		$where .= " and goods_cate_id = '" . $cate . "'";
	}
	if ($brand) {
		$where .= " and goods_brand_id = '" . $brand . "'";
	}

	load_file('page_class', LIB_PATH);
	$MyPage = new Mypage($db);
	$field = '*';
	$data_ary = $MyPage->from_sql($field, $main->get_table_name(), $where, "goods_id DESC", intval($_GET['cpage']), "20");
	$page = $MyPage->get_page($data, $cf_self_page . '?dpt=' . $cf_dpt . '&skey=' . $skey . '&cate=' . $cate . '&brand=' . $brand . '&start=' . $start.'&end='.$end);
	
	
	//echo $MyPage->get_sql();
	//dump($data_ary);
	break;
    case "add":
	if ($cf_ispost) {	
		
		$goods_color = implode(",",$_POST['goods_color']);
		$goods_images = implode(",",$_POST['goods_images']);
		$item_title = $_POST['item_title'];
		if($item_title){
			$item_price = $_POST['item_price'];
			$item_original_price = $_POST['item_original_price'];
			$item_ary = array();
			foreach($item_title as $k=>$v){
				$item_ary[$k]['title'] = $v;
				$item_ary[$k]['price'] = $item_price[$k];
				$item_ary[$k]['original_price'] = $item_original_price[$k];
					
			}			
			$item_ary = serialize($item_ary);
		}
		
		$data = $db->getFormData($main->get_table_name(),$_POST);
		$data['goods_color'] = $goods_color;
		$data['goods_images'] = $goods_images;
		$data['goods_property_item'] = $item_ary;
		$data['goods_create_time'] = date("Y-m-d H:i:s");
		$data['goods_editor'] = $cf_admin_info['admin_name'];		
		
		
	    $result = $main->insert_data($data);
		
	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		$more = $_SERVER['HTTP_REFERER'] ? '<a onclick="my_close()" href="' . $_SERVER['HTTP_REFERER'] . '">[继续添加]</a>' : '';
		my_jump('添加成功' . $more, '', $return_page, 3);
	    } else {
		my_jump('添加失败');
	    }
	} else {

	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row['goods_image'] = array();
		load_file('brand_class', LIB_PATH);		
		$brand = new Brand($db);
		//获取商品品牌数组
		$where = "brand_is_show = '1'";		
		$data = $brand->get_data_array($where,'brand_sort_num ASC,brand_id DESC','','brand_id,brand_name');		
		$brand_list = array(''=>'请选择品牌');
		if($data){
			foreach($data as $v){
				$brand_list[$v['brand_id']] = $v['brand_name'];	
			}
		}
		$row['brand_list'] = $brand_list;
		
		//获得商品分类数组
		load_file('goods_class', LIB_PATH);
		$goods = new Goods($db);
		$data = $goods->get_cate_array('1','cate_sort_num ASC,cate_id DESC','','cate_id,cate_name');
		$cate_list = array(''=>'请选择分类');
		if($data){
			foreach($data as $v){
				$cate_list[$v['cate_id']] = $v['cate_name'];	
			}
		}
		$row['cate_list'] = $cate_list;
		
		//获得颜色数组
		load_file('color_class', LIB_PATH);
		$color = new Color($db);
		$row['color_list'] = $color->get_data_array('1','color_id DESC','','color_id,color_name');
		
		//快递模板数据
		load_file('expressfee_class',LIB_PATH);
		$expressfee = new ExpressFee($db);
		$data = $expressfee->get_data_array('1','fee_id DESC','','fee_id,fee_title');
		$expressfee_list = array(''=>'请选择邮费模板');
		if($data){
			foreach($data as $v){
				$expressfee_list[$v['fee_id']] = $v['fee_title'];	
			}
		}
		$row['expressfee_list'] = $expressfee_list;		
	}

	break;
    case "edit":
	if ($cf_ispost) {
	    $data = $db->getFormData($main->get_table_name(),$_POST);
		$goods_photo;
		for($i=0;$i<5;$i++){
			$key = 'goods_image'.$i;
			if($_POST[$key]){
				
				$goods_photo .= $goods_photo ? '|'.$_POST[$key] : $_POST[$key];
			}
			
		}
		
		$goods_options = array();
		for($i=1;$i<=4;$i++){			//默认4个属性			
			$key = 'option'.$i;
			$key_value = 'option'.$i.'_value';
			if($_POST[$key]){
				$item = array();
				$item['title'] = $_POST[$key];	
				foreach($_POST[$key_value] as $v){
					if(trim($v))
					$item['option'][]=$v;
				}
				$goods_options[$i] = $item;
			}
		}
		if($goods_options){
			$goods_options = addslashes(json_encode($goods_options));
		}
		
		
		/*if($_POST['goods_option_type']){
			if($_POST['color_code']){											//颜色处理
				foreach($_POST['color_code'] as $v){
					if(trim($v))
					$data['goods_color'] .= $data['goods_color'] ? ','.trim($v) : trim($v);
				}
			}
			if($_POST['size_code']){											//尺寸处理
				foreach($_POST['size_code'] as $v){
					if(trim($v))
					$data['goods_size'] .= $data['goods_size'] ? ','.trim($v) : trim($v);
				}
			}
		}*/
		
		if($_POST['item_title']){											//套餐处理
			$item = array();
			foreach($_POST['item_title'] as $k=>$v){
				$title = trim($v);
				$price = trim($_POST['item_price'][$k]);
				
				if($title && $price){
					$item[]= array('title'=>$title,'price'=>round($price,2));
				}
			}
			if($item){
				$goods_package = addslashes(json_encode($item));	
			}
		}
		
		$data['goods_cate'] = intval($_POST['cate1']); 
		$data['goods_sub_cate'] = intval($_POST['cate2']); 
		$data['goods_index_photo'] = get_thumb_name($_POST['goods_image'.$_POST['goods_index_photo']]);
		$data['goods_photo'] = $goods_photo;
		$data['goods_package'] = $goods_package; 
		$data['goods_edit_time'] = date("Y-m-d H:i:s");
	    $data['goods_options'] = $goods_options;
	    //$db->debug(1,1);
	    $result = $main->update_data($data, "goods_id='$id'");

	    if ($result) {
		$return_page = $_SESSION['pre_url'] ? $_SESSION['pre_url'] : $cf_self_page;
		unset($_SESSION['pre_url']);
		    my_success('修改成功', '', $return_page, 3);
	    } else {
		    my_error('修改失败');
	    }
	} else {
		$cate = $cf_info_var_common['goods_cate'];
	    $smarty->mypage($cf_controller, $cf_action, 0);
	    $_SESSION['pre_url'] = $cf_pre_page ? $cf_pre_page : '';
		$row = $main->get_data_row($id);
	    if (!$row)
		my_jump('沒有此信息');
		if($row['goods_photo'])
        $row['goods_image'] = explode("|",$row['goods_photo']);
		$row['goods_index_value'] = array_search(str_replace("thumb","",$row['goods_index_photo']),$row['goods_image']);
		if($row['goods_package'])
        $row['package'] = json_decode( stripslashes($row['goods_package']),true);
		if($row['goods_options'])
        $row['options'] = json_decode( stripslashes($row['goods_options']),true);
		
		
		
		//dump($row);
	    //my_print($row['item']);
	   
	}
	break;
    case "del":
	$result = $main->del_data("goods_id='$id'");
	if ($result) {
	    my_jump('删除成功', '', $return_page, 1);
	} else {
	    my_jump('删除失败!');
	}

	break;
	case "detail":
	$smarty->mypage($cf_controller, $cf_action, 0);
	$row = $main->get_data_row($id);
	if (!$row)
	my_jump('沒有此信息');
	if($row['goods_photo']){
    $img_ary = explode("|",$row['goods_photo']);
		foreach($img_ary as $v){
			$img_list .= '<a href="'.$v.'" rel="lightbox-group" title="'.strip_tags($text_ary[$k]).'"><img src="'.get_thumb_name($v).'" height="30" /></a>&nbsp;&nbsp;'; 
		}
	}
	$row['img'] = $img_list;
	$row['cate'] = get_cate($row['goods_cate'],$row['goods_sub_cate']);
		if($row['goods_package'])
        $row['package'] = json_decode( stripslashes($row['goods_package']),true);
		if($row['goods_options'])
        $row['options'] = json_decode( stripslashes($row['goods_options']),true);
	//dump($row);
	break;  
    case "all":
		$alltype = $_POST['alltype'];
		$idlist = $_POST['idlist'];
		if($idlist){
			$list = implode(",",$idlist);
			switch($alltype){
				case "allspecial":
					$data = array('goods_isspecial'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_isspecial='0' ");
				break;
				case "allunspecial":
					$data = array('goods_isspecial'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_isspecial='1' ");
				break;
				case "allhot":
					$data = array('goods_ishot'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_ishot='0' ");
				break;
				case "allunhot":
					$data = array('goods_ishot'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_ishot='1' ");
				break;
				case "allup":
					$data = array('goods_status'=>'1');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_status='0' ");
				break;
				case "alldown":
					$data = array('goods_status'=>'0');
					$result = $main->update_data($data,"goods_id in (".$list.") and goods_status='1' ");
				break;
				case "alldel":
					$result = $main->del_data("goods_id in (".$list.") and goods_status='0'");
				break;
				default:
					my_jump('请指定操作方式');
				break;
			}	
			if($result)
				my_jump('操作完成','',$cf_self_page,1);
			else
				my_jump('操作失败');
			
		}else{
			my_jump('输入的参数有误');	
		}
	break;
	
	default:
	my_jump('非法操作!');
	break;
}


$smarty->assign(array(
    'id' => $id,
    'row' => $row,
    'data_ary' => $data_ary,
    'page' => $page,
    'self' => $self,
    'skey' => $skey, 
    'cate' => $cate,
    'brand' => $brand,
	'start' => $start,
	'end' => $end,
	'brand_ary' => add_array_item(array('' => '请选择品牌'), $cf_brand),
	'cate_ary' => add_array_item(array('' => '请选择分类'), $cf_goods_cate),	
	'expressfee_ary' => add_array_item(array('' => '请选择邮费模板'), $cf_expressfee),
	'color_ary' => add_array_item(array('' => '请选择邮费模板'), $cf_color),
));

$smarty->mydisplay();
?>